﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PropertyManagementSystemAPI.Models;

namespace PropertyManagementSystemAPI.Controllers.Owner
{
    /// <summary>
    /// 停车管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "业主")]
    public class OwParkingController : ControllerBase
    {
        public readonly IFreeSql db;//声明一个名为db的只读字段，类型为Ifreesql接口，用于表示数据库连接实例
        public OwParkingController(IFreeSql _db)
        {
            this.db = _db;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="ys"></param>
        /// <param name="ym"></param>
        /// <param name="name"></param>
        /// <param name="pCellId"></param>
        /// <returns></returns>
        [HttpGet]
        public object Get(int ys, int ym, string? name, int pCellId)
        {
            int pageSize = ys; //页数
            int pageIndex = ym;//页码
            string str = HttpContext.Session.GetString("user");
            var ww = db.Select<ParkingSpaces, CellInformation>()
                        .LeftJoin((a, b) => a.PCellId == b.CellId)
                        .WhereIf(!string.IsNullOrEmpty(name), (a, b) => b.CellName == name)
                        .Where((a, b) => a.PCellId == Convert.ToInt32(str))
                        .ToList((a, b) => new { a.PId, a.PCellId, a.PName, a.PCreateTime, a.PUserId, a.PUseTime, a.PUseState, a.PState, b.CellName, b.CellId });
            int totalCount = ww.Count();//总条数
            var list = ww.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
        /// <summary>
        /// 根据小区ID查询房产信息
        /// </summary>
        /// <param name="pCellId"></param>
        /// <returns></returns>
        [HttpGet("GetByCellId")]
        public object GetByCellId(int pCellId)
        {
            string str = HttpContext.Session.GetString("user");
            var list = db.Select<ParkingSpaces, CellInformation>()
                .LeftJoin((a, b) => a.PCellId == b.CellId)
                .Where((a, b) => a.PCellId == pCellId && a.PUserId == Convert.ToInt32(str))
                .ToList((a, b) => new { a.PId, a.PCellId, a.PName, a.PCreateTime, a.PUserId, a.PUseTime, a.PUseState, a.PState, b.CellName, b.CellId });
            int totalCount = list.Count();//总条数
            return new
            {
                TotalCount = totalCount,
                Data = list
            };
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="ww"></param>
        /// <returns></returns>
        [HttpPost]
        public int Add(ParkingSpaces ww)
        {
            var infos = new ParkingSpaces()
            {
                PId = ww.PId,
                PCellId = ww.PCellId,
                PCreateTime = ww.PCreateTime,
                PName = ww.PName,
                PState = ww.PState,
                PUserId = ww.PUserId,
                PUseState = ww.PUseState,
                PUseTime = ww.PUseTime,
            };
            var m = db.Insert(infos).ExecuteAffrows();
            return m;
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ww"></param>
        /// <returns></returns>
        [HttpPut]
        public int Edit(int id, ParkingSpaces ww)
        {
            var m = db.Update<ParkingSpaces>().Set(a => new ParkingSpaces
            {
                PId = ww.PId,
                PCellId = ww.PCellId,
                PCreateTime = ww.PCreateTime,
                PName = ww.PName,
                PState = ww.PState,
                PUserId = ww.PUserId,
                PUseState = ww.PUseState,
                PUseTime = ww.PUseTime,
            }).Where(a => a.PId == id).ExecuteAffrows();
            return m;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public int Delete(int id)
        {
            var affectedRows = db.Delete<ParkingSpaces>()
                .Where(a => a.PId == id)
                .ExecuteAffrows(); //id删除
            return affectedRows;
        }
    }
}
